﻿
// Get control's left position
function getLeft(el)
{   
     return el == null ? 0 : (el.offsetLeft + getLeft(el.offsetParent));   
}

// Get controls's top position
function getTop(el)
{   
     return el == null ? 0 : (el.offsetTop + getTop(el.offsetParent));   
}

function UploadForm(){}

// Self defined singleton constructor
UploadForm.instance = null;
UploadForm.getInstance = function()
{
    if (UploadForm.instance == null)
    {
        UploadForm.instance = new UploadForm();
        UploadForm.instance._currentDiv = null;
        UploadForm.instance._targetControlId = null;
        UploadForm.instance._origionalValues = new Array();
    }
    return UploadForm.instance;
}

// On Show Div
UploadForm.prototype.ShowDiv = function(divId, hostControlId)
{
    // Keep only one Div visible
    if (UploadForm.getInstance()._currentDiv != null)
    {
        UploadForm.getInstance()._currentDiv.style.display = "none";
        if (UploadForm.getInstance()._targetControlId != null)
        {
            var hostControl = document.getElementById(hostControlId);
        }
        if (UploadForm.getInstance()._currentDiv.id == divId)
        {
            UploadForm.getInstance()._currentDiv = null;
            return;
        }
    }
    var div = document.getElementById(divId);
    UploadForm.getInstance()._currentDiv = div;
    UploadForm.getInstance()._targetControlId = hostControlId;
    
    // Override global onmousedown event
    document.body.onmousedown = function()
    {
        if (UploadForm.getInstance()._currentDiv == null)
        {
            return;
        }
        // If SourceElement is not current div, then close current div
        var pElement = event.srcElement;
        
        // Ignore target control click
        if (pElement.id == UploadForm.getInstance()._targetControlId) 
        {
            return;
        }
        
        // Ignore inner current div control
        var isFind = false;
        while (pElement != null)
        {
            if (pElement.id == UploadForm.getInstance()._currentDiv.id)
            {
                isFind = true;
                break;
            }
            pElement = pElement.parentElement;
        }
        
        // If the control is outside current div control
        // hide the div
        if (!isFind)
        {
            UploadForm.getInstance()._currentDiv.style.display = "none";
            UploadForm.getInstance()._currentDiv = null;
        }
    }
    var hostControl = document.getElementById(hostControlId);
    if (div.style.display != "block")
    {
        div.style.display = "block";
        adjustPos(div, hostControl);
    }
}

function adjustPos(div, hostControl)
{
    // Calculate top
    div.style.top = getTop(hostControl) + hostControl.offsetHeight + 7 + "px";
    
    // Calculate left
    var hostLeft = getLeft(hostControl)
    var divWidth = div.offsetWidth;
    var bestOffset = 150;
    if (divWidth < bestOffset)
    {
        div.style.left = hostLeft - 10 + "px";
        return;
    }
    if (hostLeft + bestOffset > document.body.offsetWidth)
    {
        div.style.left = document.body.offsetWidth - divWidth - 10 + "px";
    }
    else
    {
        div.style.left = hostLeft + bestOffset - divWidth + "px";
    }
}
        
// Show or Hide Query Form
function ShowUpLoadDIV(id)
{
    UploadForm.getInstance().ShowDiv(id, event.srcElement.id);
    return false;
}
